#!/usr/bin/env python

import sys
import linecache
import re

addPattern = re.compile(r'''
                    (\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})
                    ''', re.VERBOSE)


def count(filename):
    octetsTotal = 0
    readoctet = False
    octet = 0
    datalist =[]

    def jumpDuration():
        fsock.next()
        fsock.next()
        fsock.next()
        fsock.next()

    try:
        f = open(filename, "r", 0)
        fsock = enumerate(f)
        
        while 1:
            try:
                count,line = fsock.next()
            except StopIteration:
                break
            finally:
                pass


            if "Octets" in line:
                octet = int(line.split()[1].strip())
                data = {}
                data['number'] = count-1
                addGroup = addPattern.search(fsock.next()[1]).groups()[0:4]
                data['srcaddr'] = '.'.join( "%s" % d for d in addGroup)
                addGroup = addPattern.search(fsock.next()[1]).groups()[0:4]
                data['destaddr'] = '.'.join( "%s" % d for d in addGroup)

                inputInt = fsock.next()[1]
                if "112" in inputInt:
                    octetsTotal +=octet
                    jumpDuration()
                    data['srcport'] =fsock.next()[1].split()[1].strip()
                    data['destport'] =fsock.next()[1].split()[1].strip()
                    datalist.append(data)
    finally:
        f.close()



        
    return octetsTotal,datalist

            
if __name__ == "__main__":
    filename = sys.argv[1]
    if filename :
        (total, datalist) = count(filename)
        print ('Octets Totl = {0}'.format(total))
        for f in datalist:
            print('Line = {0}'.format(f['number']))
            print('Srcaddr = {0}'.format(f['srcaddr']))
            print('Destaddr = {0}'.format(f['destaddr']))
            print('Srcport = {0}'.format(f['srcport']))
            print('Destport = {0}\n'.format(f['destport']))

    else:
        print "pls define the filename!"

